#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl
from modules.connect_db import engine, UserProfile, RemoteUser, Host, Group, AuditLog, \
    BindHost, AuditLog, Admin_user, Group_with_UserProfile, BindHost_with_Group
from sqlalchemy.orm import sessionmaker
from core.ssh_login import Login_in
session_connet = sessionmaker(bind=engine)
session = session_connet()


class User_run(object):
    def __init__(self, user):
        self.user = user
        user = session.query(UserProfile).filter(UserProfile.username == self.user).first()
        self.user_id = user.id

    def menu(self):
        choise_menu = '''
                   \033[1;31m
                      ---------Welcome  %s----------
                      1.请选择主机组
                      2.查询操作日志
                      3.退出
                    \033[0m
                    ''' % self.user
        menu_dict = {
            '1': User_run.choise_host,
            '2': User_run.select_cmd,
            '3': 'exit'
        }

        while True:
            print(choise_menu)
            choise = input('请选择>>>')
            if choise in menu_dict:
                if choise == '3':
                    exit()
                else:
                    menu_dict[choise](self)
            else:
                print('输入错误！')

    def choise_host(self):
        group_list = session.query(Group_with_UserProfile).\
            filter(Group_with_UserProfile.userprofile_id == self.user_id).all()  # 查出关联次用户的主机组
        for index, i in enumerate(group_list):
            obj_group = session.query(Group).filter(Group.id == i.group_id).first()  # 查看主机组对应的名字
            print(index, obj_group.name)
        group_choise = input('请选择主机组>>>')
        if group_choise.isdigit():
            if group_list[int(group_choise)]:
                group_id = group_list[int(group_choise)].group_id
                bindhost_list = session.query(BindHost_with_Group).\
                    filter(BindHost_with_Group.group_id == group_id).all()  # 查看主机组下绑定的主机
                for index, f in enumerate(bindhost_list):
                    bindhost_obj = session.query(BindHost).filter(BindHost.id == f.bindhost_id).first()
                    print(index, bindhost_obj.host.ip_addr, bindhost_obj.remoteuser.username)  # 列出主机详细信息
                bindhost_choise = input('请选择主机>>>')
                if bindhost_choise.isdigit():
                    if bindhost_list[int(bindhost_choise)]:
                        bindhost_id = bindhost_list[int(bindhost_choise)].bindhost_id  # 获取主机id
                        bindhost_use = session.query(BindHost).filter(BindHost.id == bindhost_id).first()  # 获取该主机信息
                        ssh_obj = Login_in(self.user_id, bindhost_use)  # 将用户id和需要登录的主机信息传送给登录模块
                        ssh_obj.login_start()

    def select_cmd(self):
        group_list = session.query(Group_with_UserProfile). \
            filter(Group_with_UserProfile.userprofile_id == self.user_id).all()  # 查出关联次用户的主机组
        for index, i in enumerate(group_list):
            obj_group = session.query(Group).filter(Group.id == i.group_id).first()  # 查看主机组对应的名字
            print(index, obj_group.name)
        group_choise = input('请选择主机组>>>')
        if group_choise.isdigit():
            if group_list[int(group_choise)]:
                group_id = group_list[int(group_choise)].group_id
                bindhost_list = session.query(BindHost_with_Group). \
                    filter(BindHost_with_Group.group_id == group_id).all()  # 查看主机组下绑定的主机
                for index, f in enumerate(bindhost_list):
                    bindhost_obj = session.query(BindHost).filter(BindHost.id == f.bindhost_id).first()
                    print(index, bindhost_obj.host.ip_addr, bindhost_obj.remoteuser.username)  # 列出主机详细信息
                bindhost_choise = input('请选择主机>>>')
                if bindhost_choise.isdigit():
                    if bindhost_list[int(bindhost_choise)]:
                        bindhost_id = bindhost_list[int(bindhost_choise)].bindhost_id  # 获取主机id
                        log_list = session.query(AuditLog).filter(AuditLog.user_id == self.user_id,
                                                                  AuditLog.bind_host_id == bindhost_id).\
                            order_by(AuditLog.date.desc()).all()  # 根据用户id和主机id，按时间倒序排列查询操作日志
                        for i in log_list:
                            print(i.date, i.user_profile.username, i.cmd)